﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Baosight.Platform.Core.UI.Controls;

namespace Baosight.ZNH.ZMCommon.Prompt
{
    public partial class Prompts : UserControl
    {
        private static Prompts instance = new Prompts();

        private Prompts()
        {
            InitializeComponent();
            Init();
        }

        public static Prompts getInstance()
        {
            return instance;
        }

        public void SetMsg(string msg)
        {
            this.textMessage.Text = msg;
        }

        #region 基础方法
        /// <summary>
        /// 初始化位置
        /// </summary>
        private void Init()
        {
            var height = Application.Current.Host.Content.ActualHeight < 100 ? 600 : Application.Current.Host.Content.ActualHeight;
            var width = Application.Current.Host.Content.ActualWidth < 100 ? 800 : Application.Current.Host.Content.ActualWidth;

            var popupY = 30;
            var popupX = (width - 450) / 2;

            this.PromptPopup.HorizontalOffset = popupX;
            this.PromptPopup.VerticalOffset = popupY;
        }

        /// <summary>
        /// 是否显示
        /// </summary>
        public Boolean IsOpenThis
        {
            get { return this.PromptPopup.IsOpen; }
            set { this.PromptPopup.IsOpen = value; }
        }

        /// <summary>
        /// 关闭popup显示
        /// </summary>
        public void Close()
        {
            if (IsOpenThis)
            {
                timer.Stop();
                this.PromptPopup.IsOpen = false;
            }
        }

        #endregion

        #region 定时器

        DispatcherTimer timer = new DispatcherTimer();

        /// <summary>
        /// 显示Popup
        /// </summary>
        /// <param name="times"></param>
        public void Show(int times)
        {
            if (!IsOpenThis)
            {
                Init();//重新计算位置
                this.PromptPopup.IsOpen = true;
                timer.Interval = new TimeSpan(0, 0, times);
                timer.Start();
                timer.Tick += new EventHandler(Timer_Tick);
            }
        }
        
        private void Timer_Tick(object sender, EventArgs e){
            //在这里处理定时器事件
            timer.Stop();
            this.PromptPopup.IsOpen = false;
        }

        #endregion

        #region 鼠标事件

        private void TextBlock_MouseEnter(object sender, MouseEventArgs e)
        {
            this.textClose.FontWeight = FontWeights.Bold;
        }

        private void TextBlock_MouseMove(object sender, MouseEventArgs e)
        {
            this.textClose.FontWeight = FontWeights.Bold;
        }

        private void TextBlock_MouseLeave(object sender, MouseEventArgs e)
        {
            this.textClose.FontWeight = FontWeights.Normal;
        }

        private void TextBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            timer.Stop();
            this.PromptPopup.IsOpen = false;
        }
        #endregion
    }
}
